﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Fc
{
    public partial class JinDu4_In : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime d;
            if (!DateTime.TryParse(this.RiQi.Text, out d))
            {
                if (DateTime.Now.Hour > 12)
                {
                    this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                }
                else
                {
                    RiQi.Text = DateTime.Today.AddDays(-1).ToString("yyyy年M月d日");
                }
            }
            if (this.IsPostBack)
            {

            }
            else
            {
                string id = Request.QueryString["id"];
                int billid;
                if (id == null)
                {
                }
                else if (int.TryParse(id, out billid))
                {
                    ViewState["BillID"] = billid;
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = 
@"select 日期, 产品类别ID, 工段, 投入塑质, 投入铁质, 内套, 外套, 内套废, 外套废, 注塑1, 注塑2, 注塑3, 注塑4, 注废1, 注废2, 注废3, 注废4, 车丝1, 
车丝2, 车丝3, 车丝4, 车废1, 车废2, 车废3, 车废4, 入库 from [生产进度4] where id=" + billid;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.BanCi.SelectedValue = Reader["工段"].ToString();
                        this.ChanPin.SelectedValue = Reader["产品类别ID"].ToString();
                        this.RiQi.Text = ((DateTime)Reader["日期"]).ToLongDateString();
                        this.SuZhi.Text = Reader["投入塑质"].ToString();
                        this.TieZhi.Text = Reader["投入铁质"].ToString();
                        this.NeiTao.Text = Reader["内套"].ToString();
                        this.NeiTaoFei.Text = Reader["内套废"].ToString();
                        this.WaiTao.Text = Reader["外套"].ToString();
                        this.WaiTaoFei.Text = Reader["外套废"].ToString();
                        this.ZhuSu1.Text = Reader["注塑1"].ToString();
                        this.ZhuSu2.Text = Reader["注塑2"].ToString();
                        this.ZhuSu3.Text = Reader["注塑3"].ToString();
                        this.ZhuSu4.Text = Reader["注塑4"].ToString();
                        this.ZhuFei1.Text = Reader["注废1"].ToString();
                        this.ZhuFei2.Text = Reader["注废2"].ToString();
                        this.ZhuFei3.Text = Reader["注废3"].ToString();
                        this.ZhuFei4.Text = Reader["注废4"].ToString();
                        this.CheSi1.Text = Reader["车丝1"].ToString();
                        this.CheSi2.Text = Reader["车丝2"].ToString();
                        this.CheSi3.Text = Reader["车丝3"].ToString();
                        this.CheSi4.Text = Reader["车丝4"].ToString();
                        this.CheFei1.Text = Reader["车废1"].ToString();
                        this.CheFei2.Text = Reader["车废2"].ToString();
                        this.CheFei3.Text = Reader["车废3"].ToString();
                        this.CheFei4.Text = Reader["车废4"].ToString();
                        this.RuKu.Text = Reader["入库"].ToString();

                    }
                    Reader.Close();
                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }

            }
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            object billid = ViewState["BillID"];

            int banci;
            if (!int.TryParse(this.BanCi.Text, out banci))
            {
                this.Message.Text = "·请选择班次";
                return;
            }
            int chanpin;
            if (!int.TryParse(this.ChanPin.Text, out chanpin))
            {
                this.Message.Text = "·请选择产品";
                return;
            }
            DateTime riqi;
            if (!DateTime.TryParse(this.RiQi.Text, out riqi))
            {
                this.Message.Text = "·输入的日期有误";
                return;
            }

            double suzhi;
            if (!double.TryParse(this.SuZhi.Text, out suzhi))
            {
                this.Message.Text = "·投入塑质重量输入有误";
                return;
            }
            double tiezhi;
            if (!double.TryParse(this.TieZhi.Text, out tiezhi))
            {
                this.Message.Text = "·投入铁质重量输入有误";
                return;
            }

            int neitao;
            if (!int.TryParse(this.NeiTao.Text, out neitao))
            {
                this.Message.Text = "·焊接内套合格数目输入有误";
                return;
            }

            int neitaofei;
            if (!int.TryParse(this.NeiTaoFei.Text, out neitaofei))
            {
                this.Message.Text = "·焊接内套不合格数目输入有误";
                return;
            }

            int waitao;
            if (!int.TryParse(this.WaiTao.Text, out waitao))
            {
                this.Message.Text = "·焊接外套合格数目输入有误";
                return;
            }

            int waitaofei;
            if (!int.TryParse(this.WaiTaoFei.Text, out waitaofei))
            {
                this.Message.Text = "·焊接外套不合格数目输入有误";
                return;
            }


            int[] zhusu = new int[4];
            int[] zhufei = new int[4];
            string zhusufield = "";
            string zhufeifield = "";
            string zhusuvalue = "";
            string zhufeivalue = "";

            int[] chesi = new int[4];
            int[] chefei = new int[4];
            string chesifield = "";
            string chefeifield = "";
            string chesivalue = "";
            string chefeivalue = "";


            int i;
            for (i = 1; i <= 4; i++)
            {
                if (!int.TryParse(((TextBox)this.FindControl("ZhuSu" + i)).Text, out zhusu[i - 1]))
                {
                    this.Message.Text = "·" + i + "#注塑数目输入有误";
                    return;
                }
                if (!int.TryParse(((TextBox)this.FindControl("ZhuFei" + i)).Text, out zhufei[i - 1]))
                {
                    this.Message.Text = "·" + i + "#注塑废品数目输入有误";
                    return;
                }

                if (!int.TryParse(((TextBox)this.FindControl("CheSi" + i)).Text, out chesi[i - 1]))
                {
                    this.Message.Text = "·" + i + "#车丝数目输入有误";
                    return;
                }
                if (!int.TryParse(((TextBox)this.FindControl("CheFei" + i)).Text, out chefei[i - 1]))
                {
                    this.Message.Text = "·" + i + "#车丝废品数目输入有误";
                    return;
                }

                if (billid == null)
                {
                    zhusufield += "[注塑" + i + "],";
                    zhusuvalue += zhusu[i - 1] + ",";
                    chesifield += "[车丝" + i + "],";
                    chesivalue += chesi[i - 1] + ",";

                    zhufeifield += "[注废" + i + "],";
                    zhufeivalue += zhufei[i - 1] + ",";
                    chefeifield += "[车废" + i + "],";
                    chefeivalue += chefei[i - 1] + ",";
                }
                else
                {
                    zhusuvalue += "[注塑" + i + "]=" + zhusu[i - 1] + ",";
                    chesivalue += "[车丝" + i + "]=" + chesi[i - 1] + ",";
                    zhufeivalue += "[注废" + i + "]=" + zhufei[i - 1] + ",";
                    chefeivalue += "[车废" + i + "]=" + chefei[i - 1] + ",";
                }
            }

            int ruku;
            if (!int.TryParse(this.RuKu.Text, out ruku))
            {
                this.Message.Text = "·入库套数输入有误";
                return;
            }

            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "未知用户";
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            if (billid == null)
            {
                Cmd.CommandText =
                    "insert into [生产进度4]([日期],[产品类别ID],[工段],[投入塑质],[投入铁质],[内套],[外套],[内套废],[外套废],"
                    + zhusufield + zhufeifield + chesifield + chefeifield
                    + "[入库],[填报人])values('"
                    + riqi.ToShortDateString() + "'," + chanpin + "," + banci + "," + suzhi + "," + tiezhi + "," + neitao + "," + waitao + "," + neitaofei + "," + waitaofei + ","
                    + zhusuvalue + zhufeivalue + chesivalue + chefeivalue + ruku + ",'" + op + "');\n"
                    + "select @@IDENTITY";

                Cmd.CommandType = CommandType.Text;
                int newid = (int)(Decimal)Cmd.ExecuteScalar();
                ViewState["BillID"] = newid;

            }
            else
            {
                Cmd.CommandText = "update [生产进度4] set "
                    + "[日期]='" + riqi.ToShortDateString() + "',"
                    + "[产品类别ID]=" + chanpin + ","
                    + "[工段]=" + banci + ","
                    + "[投入塑质]=" + suzhi + ","
                    + "[投入铁质]=" + tiezhi + ","
                    + "[内套]=" + neitao + ","
                    + "[外套]=" + waitao + ","
                    + "[内套废]=" + neitaofei + ","
                    + "[外套废]=" + waitaofei + ","
                    + zhusuvalue + zhufeivalue + chesivalue + chefeivalue
                    + "[入库]=" + ruku + ","
                    + "[填报人]='" + op
                    + "' where id=" + (int)billid;
               
                Cmd.ExecuteNonQuery();
            }
            //Response.Write(Cmd.CommandText);
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();
            this.Message.Text = "·保存成功";
            this.Button_New.Visible = true;

        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("JinDu4_In.aspx");
        }
    }
}
